using System;
using System.Collections.Generic;
using UnityEngine;

namespace Gj.Pathfinding
{
    public class Demo
    {
        public static void Jps()
        {
            // var instance = new SquareJPSInstance();
            var instance = new HexJPSInstance(HexMetrics.Default);
            var jps = new JPS(instance);
            // var navigable = new int[10, 10]
            // {
            //     { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
            //     { 0, 1, 0, 1, 0, 0, 0, 1, 1, 0 },
            //     { 0, 1, 1, 1, 0, 0, 0, 1, 1, 0 },
            //     { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0 },
            //     { 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 },
            //     { 0, 1, 1, 0, 0, 0, 0, 0, 1, 1 },
            //     { 0, 1, 0, 0, 0, 0, 0, 0, 1, 1 },
            //     { 0, 1, 0, 0, 0, 0, 0, 0, 1, 1 },
            //     { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
            //     { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }
            // };
            // int[,] navigable = new int[10, 10]
            // {
            //     {0,0,0,0,0,0,0,0,0,0 },
            //     {0,0,0,0,0,0,0,0,0,0 },
            //     {0,0,1,0,0,0,0,0,0,0 },
            //     {0,1,1,0,0,0,0,0,0,0 },
            //     {0,1,1,0,0,0,0,0,0,0 },
            //     {1,1,0,0,0,0,0,0,0,0 },
            //     {0,1,0,0,0,0,0,0,0,0 },
            //     {1,1,0,0,0,0,0,0,0,0 },
            //     {0,1,1,1,1,0,0,0,0,0 },
            //     {0,0,0,0,0,0,0,0,0,0 },
            // };
            var size = new Vector2Int(50, 50);
            var navigable = new int[size.x, size.y];
            for (int i = 0; i < size.x; i++)
            {
                for (int j = 0; j < size.y; j++)
                {
                    navigable[i, j] = 1;
                }
            }

            var grid = new JPSGrid(navigable);
            var result = new List<Vector2Int>();
            var r = jps.FindPath(grid, new Vector2Int(1, 1), new Vector2Int(7, 8), 1, ref result);
            Console.WriteLine(r);
            UnityEngine.Debug.LogError(r);
            foreach (var i in result)
            {
                Console.WriteLine(i);
                UnityEngine.Debug.LogError(i);
            }
            // Debug.Log();
        }

        public static void Dijkstra()
        {
            var instance = new HexJPSInstance(HexMetrics.Default);
            var navigable = new Dictionary<int, Dictionary<int, int>>();

            var d = new MultiJPS(instance, navigable);
            var points1 = new Vector2Int[]
            {
                new(5, 2),
                new(2, 5)
            };
            var polygon1 = new Vector2Int[]
            {
                new(0, 0),
                new(5, 0),
                new(5, 5),
                new(0, 5)
            };
            var area1 = d.AddArea(polygon1, points1);

            var points2 = new Vector2Int[]
            {
                new(5, 7),
                new(2, 5)
            };
            var polygon2 = new Vector2Int[]
            {
                new(0, 5),
                new(5, 5),
                new(5, 10),
                new(0, 10)
            };
            var area2 = d.AddArea(polygon2, points2);

            var points3 = new Vector2Int[]
            {
                new(7, 5),
                new(5, 7),
                new(10, 7)
            };
            var polygon3 = new Vector2Int[]
            {
                new(5, 5),
                new(10, 5),
                new(10, 10),
                new(5, 10)
            };
            var area3 = d.AddArea(polygon3, points3);

            var points4 = new Vector2Int[]
            {
                new(12, 5),
                new(10, 7)
            };
            var polygon4 = new Vector2Int[]
            {
                new(10, 5),
                new(15, 5),
                new(15, 10),
                new(10, 10)
            };
            var area4 = d.AddArea(polygon4, points4);

            var points5 = new Vector2Int[]
            {
                new(12, 5)
            };
            var polygon5 = new Vector2Int[]
            {
                new(10, 0),
                new(15, 0),
                new(15, 5),
                new(10, 5)
            };
            var area5 = d.AddArea(polygon5, points5);
            var result = new List<KeyValuePair<MultiNode, MultiArea>>();
            var c = d.NodePath(area1, area5, new Vector2Int(1, 1), new Vector2Int(12, 3), 0, ref result);
            Console.Write(c);
            foreach (var i in result) Console.WriteLine(i.Key.Point());
        }
    }
}